﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using COMPONENT.Library.Conexion;
using COMPONENT.Entity;

namespace COMPONENT.Dao
{
    public class clsCasaDAO
    {
        private clsCasaDAO()
        {

        }
        private static clsCasaDAO instancia = null;
        public static clsCasaDAO Instancia
        {
            get
            {
                if (instancia == null)
                {
                    instancia = new clsCasaDAO();
                }
                return instancia;
            }
        }


        public List<clsCasa> ListarCasas(int idUsuario)
        {
            List<clsCasa> lst = new List<clsCasa>();
            clsCasa objCasa = null;
            SqlCommand cmd = null;
            SqlDataReader dr = null;
            try
            {
                cmd = new SqlCommand("SP_LISTAR_XUSUARIO_USUARIOCASA", clsConexion.GetConexion());
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@idUsuario", idUsuario);
                dr = cmd.ExecuteReader();

                while(dr.Read()){

                    objCasa = new clsCasa();
                    objCasa.ID = Convert.ToInt32(dr["IdCasa"]);
                    objCasa.Nombre = dr["Nombre"].ToString();
                    objCasa.Imagen = dr["Imagen"].ToString();
                    objCasa.Estado = Convert.ToBoolean(dr["Estado"]);

                    lst.Add(objCasa);
                }


            }
            catch (Exception ex)
            {
                lst = null;
                cmd.Connection.Close();
            }
            finally
            {
                cmd.Connection.Close();
            }
            return lst;
        
        }

        public clsCasa BuscarCasaxId(int idCasa)
        {

            clsCasa objCasa = null;
            SqlDataReader dr = null;
            SqlCommand cmd = null;
            try
            {
                cmd = new SqlCommand("SP_BUSCAR_XID_CASA", clsConexion.GetConexion());
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@idCasa", idCasa);
                dr = cmd.ExecuteReader();
                if (dr.Read()) {
                    objCasa = new clsCasa();
                    objCasa.ID = Convert.ToInt32(dr["IdCasa"]);
                    objCasa.Nombre = dr["Nombre"].ToString();
                    objCasa.Estado = Convert.ToBoolean(dr["Estado"]);
                }

            }
            catch (Exception ex)
            {
                objCasa = null;
                cmd.Connection.Close();
            }
            finally
            {
                cmd.Connection.Close();
            }
            return objCasa;

        }


    }
}
